package com.example.ck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ck.dao.ProjectDao;
import com.example.ck.entity.Project;
import com.example.ck.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 项目申报表(Project)表服务实现类
 *
 * @author makejava
 * @since 2024-05-01 12:14:44
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectDao projectDao;

    /**
     * 查询所有数据
     *
     * @return
     */
    @Override
    public List<Project> findAll() {
        return projectDao.selectList(null);
    }

    /**
     * 根据id查询数据
     *
     * @param id
     * @return
     */
    @Override
    public Project findById(Integer id) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        return projectDao.selectOne(qw);
    }

    /**
     * 新增数据
     *
     * @param project
     * @return
     */
    @Override
    public boolean addProject(Project project) {
        return projectDao.insert(project) > 0;
    }

    /**
     * 根据姓名获取数据
     *
     * @param username
     * @return
     */
    @Override
    public List<Project> findProjectByUserName(String username) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username", username);
        return projectDao.selectList(qw);
    }

    /**
     * 根据id删除项目
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteProjectById(Integer id) {
        return projectDao.deleteById(id) > 0;
    }

    /**
     * 根据id修改项目状态
     *
     * @param id
     * @param status
     * @return
     */
    @Override
    public boolean updateProjectStatusById(Integer id, String status) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        Project project = projectDao.selectOne(qw);
        project.setFlag(status);
        return projectDao.updateById(project) > 0;
    }

    /**
     * 根据姓名和状态查询项目
     *
     * @param userName
     * @param status
     * @return
     */
    @Override
    public List<Project> findProjectByUserNameAndStatus(String userName, String status) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("username", userName);
        qw.eq("flag", status);
        return projectDao.selectList(qw);
    }

    /**
     * 根据id修改项目的审核人数
     *
     * @param id
     * @param auditorNum
     * @return
     */
    @Override
    public void updateProjectauditorNumById(Integer id, Integer auditorNum) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        Project project = projectDao.selectOne(qw);
        project.setAuditorNum(auditorNum);
        projectDao.updateById(project);
    }

    /**
     * 修改项目招标状态
     */
    @Override
    public boolean updateProjectTenderStatusById(Integer id, String tenderStatus) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", id);
        Project project = projectDao.selectOne(qw);
        project.setTenderStatus(tenderStatus);
        return projectDao.updateById(project) > 0;
    }

    /**
     * 招标商获取所有通过审核的项目
     *
     * @return
     */
    @Override
    public List<Project> findProjectByTenderStatus() {
        QueryWrapper qw = new QueryWrapper<>();
        // 查询tender_status不是 ‘未开始’的数据
        qw.ne("tender_status", "未开始");
        return projectDao.selectList(qw);
    }
}
